package recharge_management

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type AutonomousRechargeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 人工充值
func NewAutonomousRechargeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AutonomousRechargeLogic {
	return &AutonomousRechargeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AutonomousRechargeLogic) AutonomousRecharge(req *types.AutonomousRechargeRequest) (resp *types.AutonomousRechargeResponse, err error) {
	client, _ := l.svcCtx.UserServiceRPC.UserGetByClient(l.ctx, &userrpc.UserGetByClientRequest{Id: req.VipID})
	if client == nil {
		return nil, errors.New("User ID not exists. ")
	}
	var userIds []int64
	userIds = append(userIds, req.VipID)
	balance, _ := l.svcCtx.AssetsRpc.User.UserWallet(l.ctx, &rpc.UserWalletRequest{
		UserIds: userIds,
	})
	return &types.AutonomousRechargeResponse{
		MemberID:       client.User.Id,
		MemberNickName: client.User.Nickname,
		MemberPhone:    client.User.Phone,
		Balance:        balance.DataList[0].Balance,
		MemberAccount:  client.User.Account,
		AccountType:    client.User.VipLevel,
		ValidMark:      balance.DataList[0].ValidMark,
	}, nil
}
